package com.hdoit.kylin.web.user.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.user.model.Contact;
import com.hdoit.kylin.logical.group.service.IGroupService;
import com.hdoit.kylin.logical.user.model.Linker;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.user.service.IContactService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.ContactSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

@Controller
@RequestMapping(value="/admin/user/contact/")
public class ContactController extends BaseController{

	@Override
	public String getRoutePath() {
		return "areas/admin/user/contact/";
	}
	
	private IStudentService studentService;
	private ITeacherService teacherService;
	private IContactService contactService;
	private IGroupService groupService;

	public IStudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	public ITeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public IGroupService getGroupService() {
		return groupService;
	}

	@Resource
	public void setGroupService(IGroupService groupService) {
		this.groupService = groupService;
	}

	public IContactService getContactService() {
		return contactService;
	}

	@Resource
	public void setContactService(IContactService contactService) {
		this.contactService = contactService;
	}

	private final String indexViewName="index";
	private final String outputViewName="output";
	private final String saveViewName="save";
	private final String detailedViewName="contactDetailed";
	private final String deleteViewName="delete";
	
	@RequestMapping(value=indexViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String index(Model model,ContactSearch contactSearch,PagerModel pagerModel,boolean ajaxPost){
		String groupId=teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId());
		if(null==groupId){
			groupId=studentService.queryGroupIdByLoginId(getLoginModel().getLoginId());
		}

		pagerModel.setTotalAmount(contactService.queryLinkersCountByGroupId(contactSearch,groupId));
		List<Linker> linkers=contactService.queryLinkersByGroupId(contactSearch, pagerModel, groupId);
		
		model.addAttribute("contacts",linkers);
		model.addAttribute("contactSearch",contactSearch);
		model.addAttribute(ManageContainer.PAGERKEY,pagerModel);
		
		if(ajaxPost){
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value=saveViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String save(Model model,Contact contact){
		if(StringUtils.isEmpty(contact.getContactId())){
			contact.setContactId(CommonUtils.getKeyId());
			
			String groupId=teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId());
			if(null==groupId){
				groupId=studentService.queryGroupIdByLoginId(getLoginModel().getLoginId());
			}
			if(!StringUtils.isEmpty(groupId)){
				contact.setGroupId(groupId);
			}
			else{
				contact.setGroupId("noGroup");
			}
			
			contact.setModelStatus(ModelStatus.add);
		}
		else{
			contact.setModelStatus(ModelStatus.update);
		}
		contact.setEmployeeCode(ChineseUtils.getPingYin(contact.getContactName()));
		contact.setMarkCode(ChineseUtils.getPinYinHeadChar(contact.getContactName()));
		
		ActionResult actionResult;
		if(contactService.applyChange(contact)){
			actionResult=new ActionResult();
		}
		else{
			actionResult=new ErrorResult();
		}
		
		model.addAttribute("contact", contact);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		
		return view(detailedViewName);
	}
	
	@RequestMapping(value=detailedViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String contactDetailed(Model model,String key){
		if(StringUtils.isEmpty(key)){
			Contact contact=new Contact();
			model.addAttribute("contact", contact);
			return view(detailedViewName);
		}
		
		Student student=studentService.queryModel(key);
		if(null!=student){
			model.addAttribute("student", student);
			return "areas/admin/user/student/studentDetailed";
		}
		
		Teacher teacher=teacherService.queryModel(key);
		if(null!=teacher){
			model.addAttribute("teacher", teacher);
			return "areas/admin/user/teacher/teacherDetailed";
		}
		
		Contact contact=contactService.queryModel(key);
		if(null!=contact){
			model.addAttribute("contact", contact);
			return view(detailedViewName);
		}
		
		return "";
	}
	
	@RequestMapping(value=deleteViewName,method={RequestMethod.GET,RequestMethod.POST},produces = "text/json;charset=UTF-8")
	public @ResponseBody String delete(String key){
		if(teacherService.deleteModel(key)||studentService.deleteModel(key)||contactService.deleteModel(key)){
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}
